<template>
  <div>
    <el-button-group style="margin-bottom:20px;">
      <el-button type="success" @click="xz">新增</el-button>
      <el-button type="warning" @click="sc">删除</el-button>
    </el-button-group>
    <!-- 表格 -->
    <el-table
      ref="singleTable"
      :data="list"
      border
      highlight-current-row
      @selection-change="selectionChange"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column property="name" label="快递名字"></el-table-column>
      <el-table-column property="price" label="配送费"></el-table-column>
    </el-table>
    <!-- 模态框 -->
    <el-dialog title="提示" :visible.sync="modalShow" width="30%" :before-close="qx">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="快递名字">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="配送费">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
      </el-form>
      <div style="margin-left:210px;">
        <el-button @click="qx">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapActions, mapMutations } = createNamespacedHelpers(
  "peisong"
);
export default {
  name: "Peisong",
  mounted() {
    this.getList();
  },
  data() {
    return {
      currentRow: null
    };
  },
  methods: {
    ...mapActions(["getList", "add", "del"]),
    ...mapMutations(["setModal", "setform", "setidArr"]),
    xz() {
      this.setModal(true);
      this.setform({
        name: "",
        price: ""
      });
    },
    qx() {
      this.setModal(false);
      this.setform({
        name: "",
        price: ""
      });
    },
    save() {
      this.add().then(() => {
        this.$message({
          message: "恭喜你新增成功",
          type: "success"
        });
      });
    },
    selectionChange(sel) {
      //   console.log(sel);
      this.setidArr(sel);
    },
    sc() {
      if (this.idArr.length) {
        this.del().then(() => {
          this.$message({
            message: "恭喜你删除成功",
            type: "success"
          });
        });
      }else{
        this.$message({
            message: "请选择需要删除的项目",
            type: "warning"
          });
      }
    }
  },
  computed: {
    ...mapState(["list", "form", "modalShow", "idArr"])
  }
};
</script>

<style>
</style>
